﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace moose.irc
{
    /// <summary>
    /// Manages List of Entities,
    /// This is my attempt to merge users and channels as a common denominator.
    /// This will allow me simplify private message and notice events.
    /// </summary>
    [CLSCompliant(true)]
    public class IrcEntityList : object
    {

        #region [Local Attributes]

        /// <summary>
        /// Collection of Entities
        /// </summary>
        private List<IrcEntity> m_entities;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Irc Entity List
        /// </summary>
        public IrcEntityList()
            : base()
        {
            m_entities = new List<IrcEntity>();

            return;
        }

        #endregion

        #region [Public Methods]

        /// <summary>
        /// Get an IRC Entity,
        /// will have to initially determine if we are talking about a user
        /// or a channel.
        /// </summary>
        /// <param name="entity_string">Entity String, used to search or create object</param>
        /// <returns>instance of this entity</returns>
        public IrcEntity get(string entity_string) {
            var entities = from e in m_entities //Query Entities Collection
                           where e.Name == entity_string
                           select e;
            if (entities.Count() == 1) //So, if we already have the entity, return it
                return entities.First();
            //otherwise, create it, and add it
            IrcEntity entity = create_entity(entity_string);
            m_entities.Add(entity);
            return entity;
        }
        
        /// <summary>
        /// Remove Specified Entity
        /// </summary>
        /// <param name="entity_string">Entity String, of Entity to remove</param>
        public void remove(string entity_string)
        {
            var entities = from e in m_entities //Query Entities Collection
                           where e.Name == entity_string
                           select e;
            if (entities.Count() == 1) //So, if we already have the entity, return it
                m_entities.Remove(entities.First());
            return;
        }

        /// <summary>
        /// Remove Entity
        /// </summary>
        /// <param name="entity">actual entity to remove</param>
        public void remove(IrcEntity entity)
        {
            if (m_entities.Contains(entity))
                m_entities.Remove(entity);
        }

        /// <summary>
        /// Create an entity, Autodetect entity type, based on 
        /// symbols in the entity_string,
        /// which will either be a channel name
        /// or a nickname or a full bnf host
        /// </summary>
        /// <param name="entity_string"></param>
        /// <returns></returns>
        private IrcEntity create_entity(string entity_string)
        {
            if (entity_string == null) entity_string = string.Empty;
            if (entity_string.Contains("@") && entity_string.Contains("!"))
            {
                return create_user(entity_string);
            }
            else
            {
                if (entity_string.Contains("@") || entity_string.Contains("#"))
                {
                    throw new Exception();
                    //return create_channel(entity_string);
                }
                else
                {
                    return create_user(entity_string);
                }
            }
        }

        /// <summary>
        /// Create and return new channel
        /// </summary>
        /// <param name="channel_name">name of channel</param>
        /// <returns>new instance of channel</returns>
        //private IrcEntity create_channel(string channel_name)
        //{
        //    //return new IrcChannel(channel_name);
        //}

        /// <summary>
        /// Create and return new user
        /// </summary>
        /// <param name="user_string">user string</param>
        /// <returns></returns>
        private IrcEntity create_user(string user_string)
        {
            return new IrcUser(user_string);
        }

        #endregion

    }
}
